package com.shangyueshang.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.shangyueshang.model.GoodsRevenue;
import com.shangyueshang.model.Order;
import com.shangyueshang.response.KeyValDecimal;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.util.List;

/**
 * @author: JokeyZheng
 * @email: zhengjingfeng@ruqimobility.com
 * @created: 2022/1/14 18:48
 * @version: v1.0.0
 */
@Component
public interface OrderMapper extends BaseMapper<Order> {
	
	/**
	 * 统计销售收入情况
	 *
	 * @param shop
	 * @param fromTradeDate
	 * @param toTradeDate
	 * @return
	 */
	@Select("<script>" +
			"SELECT " +
			"   shop, " +
			"   sku, " +
			"   trade_date AS tradeDate, " +
			"   SUM(COALESCE(product_sales, 0)) AS productSales, " +
			"   SUM(COALESCE(quantity, 0)) AS saleQuantity, " +
			"   SUM(COALESCE(total, 0)) AS saleRevenue, " +
			"   SUM(selling_fees) AS sellingFee, " +
			"   SUM(fba_fees) AS fbaFee " +
			"FROM " +
			"   order_info " +
			"WHERE " +
			"   shop=#{shop} " +
			"AND type ='Order' " +
			"AND trade_date <![CDATA[ >= ]]> #{fromTradeDate} " +
			"AND trade_date <![CDATA[ <= ]]> #{toTradeDate} " +
			"GROUP BY shop, sku, trade_date " +
			"ORDER BY trade_date " +
			"</script>"
	)
	List<GoodsRevenue> countSales(
			@Param("shop") String shop,
			@Param("fromTradeDate") LocalDate fromTradeDate,
			@Param("toTradeDate") LocalDate toTradeDate
	);
	
	/**
	 * 统计退款订单
	 *
	 * @param shop
	 * @param fromTradeDate
	 * @param toTradeDate
	 * @return
	 */
	@Select("<script>" +
			"SELECT " +
			"   shop, " +
			"   sku, " +
			"   trade_date AS tradeDate, " +
			"   SUM(quantity) AS returnQuantity, " +
			"   SUM(total) AS returnFee " +
			"FROM " +
			"   order_info " +
			"WHERE " +
			"   shop=#{shop} " +
			"AND type='Refund' " +
			"AND trade_date <![CDATA[ >= ]]> #{fromTradeDate} " +
			"AND trade_date <![CDATA[ <= ]]> #{toTradeDate} " +
			"GROUP BY shop, sku, trade_date " +
			"ORDER BY trade_date" +
			"</script>"
	)
	List<GoodsRevenue> countRefund(
			@Param("shop") String shop,
			@Param("fromTradeDate") LocalDate fromTradeDate,
			@Param("toTradeDate") LocalDate toTradeDate
	);
	
	
	/**
	 * 统计费用
	 *
	 * @param shop
	 * @param fromTradeDate
	 * @param toTradeDate
	 * @param feeTypes
	 * @return
	 */
	@Select("<script>" +
			"	SELECT " +
			"		type as `key`, " +
			"		SUM(total) as val " +
			"	FROM " +
			"		order_info " +
			"	WHERE " +
			"		shop=#{shop} " +
			"	AND type IN " +
			"	<foreach collection=\"feeTypes\" item=\"type\" open=\"(\" separator =\",\" close=\")\">" +
			"		#{type} " +
			"	</foreach>" +
			"	AND trade_date <![CDATA[ >= ]]> #{fromTradeDate} " +
			"	AND trade_date <![CDATA[ <= ]]> #{toTradeDate} " +
			"	GROUP BY type " +
			"</script>"
	)
	List<KeyValDecimal> countFee(
			@Param("shop") String shop,
			@Param("fromTradeDate") LocalDate fromTradeDate,
			@Param("toTradeDate") LocalDate toTradeDate,
			@Param("feeTypes") List<String> feeTypes
	);
	
}
